home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / CLMUTEX.CPP < prev    next >
C/C++ Source or Header  |  1993-07-15  |  11KB  |  368 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    clmutex.cpp
  5. //   Title:    C++ Class Libraries
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains code for the class CL_MUTEX.
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <class.hpp>
  41.  
  42.  
  43. //----------------------------------------------------------------------------
  44. //   Description:    Default constructor
  45. //    Parameters:
  46. //       Returns:    
  47. //----------------------------------------------------------------------------
  48. FN_M CL_MUTEX::CL_MUTEX()
  49. {
  50.     CL_MUTEX::Initialize(CL_INIT_CLASS);
  51. }
  52.  
  53.  
  54. //----------------------------------------------------------------------------
  55. //   Description:    Copy constructor
  56. //    Parameters:    rccl_mutex        Reference to object to copy.
  57. //       Returns:    
  58. //----------------------------------------------------------------------------
  59. FN_M CL_MUTEX::CL_MUTEX(RCCL_MUTEX rccl_mutex)
  60. {
  61.     CL_MUTEX::Initialize(CL_INIT_CLASS);
  62.     *this = rccl_mutex;
  63. }
  64.  
  65.  
  66. //----------------------------------------------------------------------------
  67. //   Description:    Destructor
  68. //    Parameters:
  69. //       Returns:    
  70. //----------------------------------------------------------------------------
  71. FN_M CL_MUTEX::~CL_MUTEX()
  72. {
  73.     CL_MUTEX::Destroy(FALSE);
  74. }
  75.  
  76.  
  77. //----------------------------------------------------------------------------
  78. //   Description:    Destroy object. Free any resources used by object.
  79. //                          Normally called by destructor.
  80. //                        Should allow multiple calls from various classes.
  81. //    Parameters:    fDestroyAll        Destroy parents also?
  82. //                                            Default is TRUE.
  83. //       Returns:    TRUE if successful.
  84. //----------------------------------------------------------------------------
  85. BOOL FN_M CL_MUTEX::Destroy(BOOL fDestroyAll)
  86. {
  87. #if OS_OS2
  88.     if (fCreate)
  89.         {
  90.         DosCloseMutexSem(hmtx);
  91.         fCreate = FALSE;
  92.         }
  93. #endif
  94.     CL_MUTEX::Initialize(CL_INIT_CLASS_VARS);
  95.     if (fDestroyAll)                            // Destroy parent.
  96.         CL_MUTEX_PARENT::Destroy(fDestroyAll);                    
  97.     return TRUE;
  98. }
  99.  
  100.  
  101. //----------------------------------------------------------------------------
  102. //   Description:    Initialize object. 
  103. //                          Normally called by constructor.
  104. //                        Should allow multiple calls from various classes.
  105. //    Parameters:    sInit        Initialization code. May be one of the following:
  106. //                                        CL_INIT_CLASS            Reset class variables and
  107. //                                                                    and dynamic allocations for
  108. //                                                                    this class only.
  109. //                                        CL_INIT_CLASS_VARS    Reset class variables for
  110. //                                                                    this class only.
  111. //                                        CL_INIT_VARS            Reset class variables for
  112. //                                                                    this class only.
  113. //                                        CL_INIT_ALL                Initialize class and all 
  114. //                                                                    parent class, including
  115. //                                                                    dynamic memory allocation.
  116. //                                    Default is CL_INIT_ALL
  117. //       Returns:    TRUE if successful.
  118. //----------------------------------------------------------------------------
  119. BOOL FN_M CL_MUTEX::Initialize(SHORT sInit)
  120. {
  121.     if (sInit == CL_INIT_VARS || sInit == CL_INIT_ALL)
  122.         CL_MUTEX_PARENT::Initialize(sInit);
  123.  
  124. #if OS_OS2
  125.     cLock = 0;
  126.     tid = 0;
  127.     fCreate = FALSE;
  128. #endif
  129.  
  130.     if (sInit == CL_INIT_CLASS_VARS || sInit == CL_INIT_VARS)
  131.         return TRUE;
  132.  
  133. #if OS_OS2
  134.     if (DosCreateMutexSem(NULL, &hmtx, 0, 0) != SUCCESS)
  135.         SetError();
  136.     else
  137.         fCreate = TRUE;
  138. #endif
  139.  
  140.     return TRUE;
  141. }
  142.  
  143.  
  144.  
  145. //----------------------------------------------------------------------------
  146. //   Description:    Check if object is in error state.
  147. //                          IsValid() && IsError() MUST NOT BE DEPENDENT ON ONE ANOTHER.
  148. //    Parameters:
  149. //       Returns:    TRUE if in error state.
  150. //----------------------------------------------------------------------------
  151. BOOL FN_M CL_MUTEX::IsError() const
  152. {
  153.     return CL_MUTEX_PARENT::IsError();
  154. }
  155.  
  156.  
  157. //----------------------------------------------------------------------------
  158. //   Description:    Check if object is valid
  159. //                          IsValid() && IsError() MUST NOT BE DEPENDENT ON ONE ANOTHER.
  160. //    Parameters:
  161. //       Returns:    TRUE if valid
  162. //----------------------------------------------------------------------------
  163. BOOL FN_M CL_MUTEX::IsValid() const
  164. {
  165.     return CL_MUTEX_PARENT::IsValid();
  166. }
  167.  
  168.  
  169. //----------------------------------------------------------------------------
  170. //   Description:    Assignment operator
  171. //                          NOTE: Don't copy object into self
  172. //    Parameters:    rccl_mutex        Reference to right value.
  173. //       Returns:    Reference to new object.
  174. //----------------------------------------------------------------------------
  175. RCCL_MUTEX FN_M CL_MUTEX::operator=(RCCL_MUTEX rccl_mutex)
  176. {
  177.     if (this != &rccl_mutex)
  178.         {
  179.         Invalid("CL_MUTEX::operator=");
  180.         }
  181.     return (RCCL_MUTEX)*this;
  182. }
  183.  
  184.  
  185. //----------------------------------------------------------------------------
  186. //   Description:    Release a mutual exclusion semapore
  187. //    Parameters:    
  188. //       Returns:    TRUE if successful.
  189. //----------------------------------------------------------------------------
  190. BOOL FN_M CL_MUTEX::Release()
  191. {
  192.     if (IsError())
  193.         return FALSE;
  194.  
  195. #if OS_OS2
  196.     Assert(tid == THREADID);
  197.  
  198.     if (!fCreate)
  199.         return FALSE;
  200.  
  201.     if (cLock)
  202.         cLock--;
  203.  
  204.     if (!cLock)
  205.         {
  206.         if (DosReleaseMutexSem(hmtx) != SUCCESS)
  207.             {
  208.             SetError();
  209.             return FALSE;
  210.             }
  211.         tid = 0;
  212.         }
  213. #endif
  214.  
  215.     return TRUE;
  216. }
  217.  
  218.  
  219. //----------------------------------------------------------------------------
  220. //   Description:    Request mutual exclusion semaphore
  221. //    Parameters:    lTimeOut        Time out period. -1L == Indefinite
  222. //                                        Default is -1L.
  223. //       Returns:    TRUE if successful.
  224. //----------------------------------------------------------------------------
  225. BOOL FN_M CL_MUTEX::Request(LONG lTimeOut)
  226. {
  227.     if (IsError())
  228.         return FALSE;
  229.  
  230. #if OS_OS2
  231.     if (!fCreate)
  232.         return FALSE;
  233.  
  234.     if (tid == THREADID)
  235.         {
  236.         cLock++;
  237.         return TRUE;
  238.         }
  239.     if (DosRequestMutexSem(hmtx, lTimeOut) != SUCCESS)
  240.         {
  241.         SetError();
  242.         return FALSE;
  243.         }
  244.     tid = THREADID;
  245.     cLock = 1;
  246. #else
  247.     NOTUSED(lTimeOut);
  248. #endif
  249.  
  250.     return TRUE;
  251. }
  252.  
  253.  
  254. //----------------------------------------------------------------------------
  255. //   Description:    Retrieve object from persistent storage
  256. //    Parameters:    pcsz        Name of object.
  257. //                        pcszSub    Sub-name of object.
  258. //                                    The first character of the name should be '~'.
  259. //                                    If NULL, no sub name is available.
  260. //                                    Default is NULL
  261. //       Returns:    TRUE if successful.
  262. //----------------------------------------------------------------------------
  263. BOOL FN_M CL_MUTEX::Retrieve(PCSZ pcsz, PCSZ pcszSub)
  264. {
  265.     NOTUSED(pcsz);
  266.     NOTUSED(pcszSub);
  267.     Invalid("CL_MUTEX::Retrieve");
  268.     return FALSE;
  269. }
  270.  
  271.  
  272. //----------------------------------------------------------------------------
  273. //   Description:    Store object to persistent storage
  274. //    Parameters:    pcsz        Name of object.
  275. //                        pcszSub    Sub-name of object.
  276. //                                    The first character of the name should be '~'.
  277. //                                    If NULL, no sub name is available.
  278. //                                    Default is NULL
  279. //       Returns:    TRUE if successful.
  280. //----------------------------------------------------------------------------
  281. BOOL FN_M CL_MUTEX::Store(PCSZ pcsz, PCSZ pcszSub)
  282. {
  283.     NOTUSED(pcsz);
  284.     NOTUSED(pcszSub);
  285.     Invalid("CL_MUTEX::Store");
  286.     return FALSE;
  287. }
  288.  
  289.  
  290. //----------------------------------------------------------------------------
  291. //   Description:    Print object value to debugging output.
  292. //    Parameters:    pccl_mutex        Pointer to dynamic object. 
  293. //                                    If NULL, static data elements are printed.
  294. //                                    Default is NULL.
  295. //                        pcsz        Name of object.
  296. //                                    If NULL, no name is displayed.
  297. //                                    Default is NULL.
  298. //                        cLevel    Display level. 
  299. //                                    Default is zero.
  300. //       Returns:
  301. //----------------------------------------------------------------------------
  302. #if COMPILE_DEBUG
  303. VOID FN_M CL_MUTEX::Print(PCCL_MUTEX pccl_mutex, PCSZ pcsz, SIZET cLevel)
  304. {
  305. #if COMPILE_TEST
  306.     OutputL(cLevel, "CL_MUTEX%s%s", (pcsz?"::":""), (pcsz?pcsz:""));
  307.     cLevel++;
  308.     if (pccl_mutex)
  309.         {
  310.         Output(" <%p>\n", pccl_mutex);
  311.         if(!pccl_mutex->IsError())
  312.             {
  313. #if OS_OS2
  314.             OutputL(cLevel, "tid = %d\n", pccl_mutex->tid);
  315.             OutputL(cLevel, "cLock = %u\n", pccl_mutex->cLock);
  316.             OutputL(cLevel, "fCreate = %u\n", pccl_mutex->fCreate);
  317. #endif
  318.             }
  319.         }
  320.     else
  321.         Output(" <NULL>\n");
  322.     CL_MUTEX_PARENT::Print((CL_MUTEX_PARENT _FAR_ *)pccl_mutex, pcsz, cLevel);
  323.     return ;
  324. #else
  325.     NOTUSED(cLevel);
  326.     NOTUSED(pccl_mutex);
  327.     NOTUSED(pcsz);
  328.     return ;
  329. #endif
  330. }
  331. #endif
  332.  
  333.  
  334. //----------------------------------------------------------------------------
  335. //   Description:    Run standard test suite on object.
  336. //    Parameters:    sTest        Test to run.
  337. //                                    If 0, run default tests.
  338. //                                    Default is 0.
  339. //       Returns:    TRUE if successful.
  340. //----------------------------------------------------------------------------
  341. #if COMPILE_DEBUG
  342. BOOL FN_M CL_MUTEX::Test(SHORT sTest)
  343. {
  344. #if COMPILE_TEST
  345.     if (sTest == 1)                            // Test 1 is always a test of storage
  346.         {
  347.         CL_MUTEX cl_mutex;
  348.         cl_mutex.Store("CL_MUTEX");
  349.         cl_mutex.Retrieve("CL_MUTEX");
  350.         CL_MUTEX::Print(&cl_mutex);
  351.         }
  352.     CL_MUTEX cl_mutex;
  353.     cl_mutex.Request();
  354.     Print(&cl_mutex);
  355.     cl_mutex.Release();
  356.     Print(&cl_mutex);
  357.     return TRUE;
  358. #else
  359.     NOTUSED(sTest);
  360.     return TRUE;
  361. #endif
  362. }
  363. #endif
  364. //----------------------------------------------------------------------------
  365. //------------------------------- End of File --------------------------------
  366. //----------------------------------------------------------------------------
  367.  
  368.